<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
      <el-breadcrumb-item>第4步：填写商品详情</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="基本信息">
        <el-descriptions :title="ruleForm.name">
          <el-descriptions-item label="品牌">
            <el-tag size="small">{{ ruleForm.brandName }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="类别" span="2">{{ ruleForm.categoryName }}</el-descriptions-item>
          <el-descriptions-item label="标题" span="3">{{ ruleForm.title }}</el-descriptions-item>
          <el-descriptions-item label="简介" span="3">{{ ruleForm.description }}</el-descriptions-item>
          <el-descriptions-item label="相册" span="3">{{ ruleForm.albumName }}</el-descriptions-item>
          <el-descriptions-item label="货号">{{ ruleForm.typeNumber }}</el-descriptions-item>
          <el-descriptions-item label="售价">{{ ruleForm.listPrice }}</el-descriptions-item>
          <el-descriptions-item label="计价单位">{{ ruleForm.brandName }}</el-descriptions-item>
          <el-descriptions-item label="库存">{{ ruleForm.stock }}</el-descriptions-item>
          <el-descriptions-item label="库存阈警值">{{ ruleForm.stockThreshold }}</el-descriptions-item>
          <el-descriptions-item label="排序序号">{{ ruleForm.sort }}</el-descriptions-item>
          <el-descriptions-item label="关键词">{{ ruleForm.keywords }}</el-descriptions-item>
          <el-descriptions-item label="标签">{{ ruleForm.tags }}</el-descriptions-item>
        </el-descriptions>
      </el-form-item>
      <el-form-item label="商品详情">
        <div id="wangEditor"></div>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="backToPreStep()">上一步</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      editor: {},
      ruleForm: {},
      rules: {}
    };
  },
  methods: {
    backToPreStep() {
      this.$router.push('add-new-3');
    },
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('localRuleForm');
      if (!localRuleFormString) {
        this.$router.push('add-new-1');
      }

      let localRuleForm = JSON.parse(localRuleFormString);
      console.log(localRuleForm);
      if (!localRuleForm.categoryId) {
        this.$router.push('add-new-1');
      } else if (!localRuleForm.brandId) {
        this.$router.push('add-new-2');
      } else if (!localRuleForm.albumId) {
        this.$router.push('add-new-3');
      }

      this.ruleForm = localRuleForm;
    },
    initWangEditor() {
      this.editor = new this.wangEditor('#wangEditor');
      this.editor.config.zIndex = 1;
      this.editor.create();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
            if (valid) {
              let detail = this.editor.txt.html();
              this.ruleForm.detail = detail;
              console.log('即将提交到服务器端的数据：');
              console.log(this.ruleForm);

              let url = 'http://localhost:9080/spu/add-new';
              console.log('url = ' + url);
              let formData = this.qs.stringify(this.ruleForm);
              console.log('formData = ' + formData);

              this.axios
                  .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
                  .post(url, formData).then((response) => {
                let responseBody = response.data;
                if (responseBody.state == 20000) {
                  this.$alert('新增SPU成功！', '提示', {
                    confirmButtonText: '确定',
                    callback: action => {
                      this.$router.push('add-new-1');
                    }
                  });
                } else {
                  this.$message.error(responseBody.message);
                }
              });
            } else {
              console.log('error submit!!');
              return false;
            }
          }
      );
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadLocalRuleForm();
    this.initWangEditor();
  }
}

// 【关于富文本编辑器wangeditor的使用】
// 1. 安装命令
//    npm i wangeditor -S
// 2. 在main.js中配置
//    import wangEditor from 'wangeditor';
//    Vue.prototype.wangEditor = wangEditor;
// 3. 在视图中使用
//    3.1. 在设计页面时，使用任何容器标签，添加ID，表示此标签是用于显示富文本编辑器的区域
//         例如：<div id="wangEditor"></div>
//    3.2. 在页面刚刚加载时，初始化此富文本编辑器
//         例如：new this.wangEditor('#wangEditor').create();
//         更建议将创建出来的对象赋值到变量上，以便于后续使用
//         则应该在data中返回时声明editor属性
//         并且：this.editor = new this.wangEditor('#wangEditor');   this.editor.create();
</script>

<style scoped>
</style>